$(function(){
	//生成大的cavas
	$('#pagemain').on('click',".pageitem",function(){
		//改变大的canvas 的布局及图片	
		var index=$(this).index();
		$(".pagemask").hide();
		$(".pageitem").removeClass("editoring")
		
		$(this).addClass("editoring");	
		$(this).find(".pagemask").show();
		
 		changeoriginalcanvas(index);		
	})
	
	//确定文字编辑	
	$('#editortextarea').on('click',".save_text ",function(){
		var currentid=$(this).attr("currentid");
		//把编辑过的文字回填
		comfrimTextToCanvas(currentid);
		$(".text_input").val('');
		
	})
	
	
	//保存并预览
	$('.originalwork').on('click',".save_mono ",function(){	
		try{
			wx.hideOptionMenu();//隐藏分享菜单		
		}catch(e){
			//TODO handle the exception
		}
		$("#loadtoast").show();
		saveCanvasOper();		
		if(UtilProperty[work_id].update_work_page.length>0){
			console.log('更新数据');
			console.log('更新数据'+JSON.stringify(UtilProperty[work_id].update_work_page));
			baseAjax('other/work/update_work_page','POST',{work_id:work_id,data:JSON.stringify(UtilProperty[work_id].update_work_page)},function(data){						
			for(var k=0;k<UtilProperty[work_id].base64image.length;k++){
				var item=UtilProperty[work_id].base64image[k];
				var iscomp=checkCoverwork(work_id,k);//检测作品中所有的控件是否都有填充了图片或文字
				if(iscomp){
					$('.work_itemlist[work_id="'+work_id+'"] .placeholder[title="'+item.title+'"]').html('<img src="'+item.imgurl+'" alt=""><div class="icon_com"><i class="weui-icon-warn"></i></div>');
				
				}else{
					$('.work_itemlist[work_id="'+work_id+'"] .placeholder[title="'+item.title+'"]').html('<img src="'+item.imgurl+'" alt="">');
				}
				$('.work_itemlist[work_id="'+work_id+'"] .placeholder[title="'+item.title+'"]').removeAttr('style');
				//canvas保存图片成功后修改ischange的值
				changeischange();
				UtilProperty[work_id].update_work_page=[];
			}
			
			})
			
		}
			document.title='请选择需要的作品';
			$("#loadtoast").hide();
			//选中的作品的数量
		 	selectWork();
			$(".originalwork").hide();	
			$(".worker_con").show();
		
	})
	
	//替换
	$('#replace_btn').on('touchend',function(e){
		
		$("#js-file").attr("onfunction","1")
		$("#js-file").trigger("click");
		//$("#js-file").attr("onfunction","1")
		   //selectFileToUpload();
          // e.preventDefault()
		
	})
	
	//旋转canvas中的图片
	$('.editor_canvas').on('click',"#roat_btn",function(){
		var currentid=canvas_img.getAttribute("currentid");
		ctxmsg[currentid].transform=Number(ctxmsg[currentid].transform)
		ctxmsg[currentid].transform += 90;
		ctxmsg[currentid].ischange=true;
		canvasimgrote();
		
	})
	
	//在编辑区域 选择需要调整的控件
	$('.editor_canvas').on('click',".editoritem ",function(){	
		$(this).addClass("editoring").siblings().removeClass("editoring");
		var currentid=$(this).attr("currentid");
		canvas_img.setAttribute("currentid",currentid);
		canvas_img.width=ctxmsg[currentid].w;
		canvas_img.height=ctxmsg[currentid].h;
		canvas_img.setAttribute("currentid",currentid);
		ctx_2d.clearRect(0,0,canvas_img.width,canvas_img.height);
		canvasdrag();
		
	})
	
	//取消编辑
	$('.editor_canvas').on('click',".canace_change",function(){
		var cas=document.getElementById("reallycanvas");
		var cxt=cas.getContext("2d");
		cxt.clearRect(0,0,cas.width,cas.height);
		ctxmsg ={};
			$(".editor_canvas").hide();
			$(".originalwork").show();
			
	})
	
	//保存编辑
	$('.editor_canvas').on('click',".comfirm_change",function(){
		saveimageeditor();	
		
	})
	
	//过滤emoji 表情
	/*$("#textarea_input").on('keyup',function(){	
		var str=$(this).val();
		var isemoji=false;
		if(emoji_reg3.test(str)){
			　　$(this).val(str.replace(emoji_reg3,""));
		 }
		
		
	})*/

})

function creatPage(w_id){
	$("#originaleditor").empty();	
	var o_canvas=  document.createElement("canvas");
		o_canvas.id="originalcanvas";
		$("#originaleditor").append(o_canvas);
	var o_cantxt=o_canvas.getContext("2d");
	o_cantxt.clearRect(0,0,o_canvas.width,o_canvas.height);
	var $obj=$("#pagemain");
	$obj.show().empty().attr("workerlistid",w_id);	
	var worker=UtilProperty[w_id].worker;	
	console.log(worker);
	console.log(w_id);
	var work_tit='正面';
	if(opentype==1){
		work_tit=$(".onactive").find(".placeholder").attr('title');
	}	
	for(var i=0; i<worker.length;i++){
		var item=worker[i];					
		var canvas=document.createElement("canvas");						
		var context=canvas.getContext("2d");
		var w=boxwindthheight.width;
		var h=boxwindthheight.height;
		var scale;		
		if(item.width / item.height > w / h) {
			scale=w/item.width;
		} else {
			scale=h/item.height;
		}
		canvas.height=item.height*scale;
		canvas.width=item.width*scale;
		var inner_scale=$obj.height()/item.height;
		canvas.setAttribute('style','width:'+canvas.width/Pixel_Ratio+'px; height:'+canvas.height/Pixel_Ratio+'px;');
		//给相应的作品添加背景图片
			
		layoutbackground(item,context,canvas.width,canvas.height,scale);		
		drawToCanvas(w_id,canvas,i,item,context,scale,true,false);
		var obj={title:item.title,pageid:item.id,imgurl:canvas.toDataURL()};
		
		if(i<UtilProperty[w_id].base64image.length){
			UtilProperty[w_id].base64image[i]=obj;	
		}else{
			UtilProperty[w_id].base64image.push(obj);
		}
		
		$(".onactive").find('.placeholder')
		var divhtm='<div class="pageitem itemflexthree '+(item.title==work_tit?'editoring':'')+'" id="'+w_id+'_'+item.id+'" title="'+item.title+'" page="'+item.page+'" style=" height:'+parseInt(item.height*inner_scale)+'px;  width:'+parseInt(item.width*inner_scale)+'px;">'+
						'<div class="page_item">'+
						//'<div class="page_item '+(i==0?'editoring':'')+'">'+
							'<img src="'+obj.imgurl+'" alt="">'+
							'<div class="pagemask _absolute" style="display:'+(item.title==work_tit?'':'none')+'">'+item.title+'</div>'+
						'</div>'+					
					'</div>';	
		$obj.append(divhtm);	
		if(opentype==2){
			$('.work_itemlist[product_id="'+product_id+'"][work_id="'+w_id+'"] .placeholder[title="'+item.title+'"]').html('<img src="'+obj.imgurl+'"/>');
		}
		if(item.title==work_tit){	
		//if(i=='0'){	
			o_canvas.width=canvas.width;
			o_canvas.height=canvas.height;
			o_cantxt.drawImage(canvas,0,0,canvas.width, canvas.height);
			var ind=$(".onactive").index();
			if(ind==-1){
				ind=0
			}
			$("#originalcanvas").attr("page",ind);
			if(opentype==1){
				$('.onfoucs[work_id="'+w_id+'"]').find(".weui-flex__item").removeClass('onactive');
			}			
			o_canvas.setAttribute('style','width:'+o_canvas.width/Pixel_Ratio+'px; height:'+o_canvas.height/Pixel_Ratio+'px;');
			//document.getElementById('originalcanvas').addEventListener("touchend", originalTouchEnd, false);
			document.getElementById('originalcanvas').addEventListener("click", originalTouchEnd, false);
		}
		
	}
	
	
}


//点击生成大的canvas
function drawToCanvas(w_id,canvas,i,item,context,scale,flag,original){//flag 是表示没有图片或控件的时候不用显示 +  或者 字等  original为true 时加载的是原图  false加载的是根据尺寸等比例缩小的图
	for(var k=0; k<item.control_list.length;k++){
			var element=item.control_list[k];
			layout_id=element.layout_id;
			if(element.control_id==1){				
				if(element.element_list instanceof Array&&element.element_list.length>0){									
						var list=element.element_list[0];
						//console.log(item,k, list)
						
						if(UtilProperty[w_id].imageObj[list.image_url]){// lym 2018.01.02 修改 图片加载 原先的的id标志转为url标志
							var pic;
							if(original){								
								pic=originalImage[w_id].imageObj[list.image_url];
							}else{
								pic=UtilProperty[w_id].imageObj[list.image_url];
							}
						 
						var _elementlist={
								imgurl:list.image_url,
								img:pic,
								image_id:list.image_id,
								x: element.coordinate_x*scale,
								y: element.coordinate_y*scale,
								w: element.width*scale,
								h: element.height*scale,
								imgx: list.cut_x*scale+element.coordinate_x*scale,
								imgy: list.cut_y*scale+element.coordinate_y*scale,
								imgw: 0,
								imgh: 0,
								imgoldx: 0,
								imgoldy: 0,
								imgoldw: pic.width,
								imgoldh: pic.height,
								transform:list.transform,								
								}
						if(_elementlist.transform>0) {		
							originalcanvasimgrote(context,_elementlist)
						}else{ 							
							drawImgByStatus(context,_elementlist);	
						}
						
					}
					
						
				}else{
					if(flag){
					var fontcss='bold  x-large Microsoft YaHei';
					//在canvas中增加提示 					
 					createReactTips(context,element,scale,'+',fontcss);
 					}
				}
				
				
			}else if(element.control_id==2){
				if(element.element_list instanceof Array&&element.element_list.length>0&&element.element_list[0].content!=""){						
						var list=element.element_list[0];	
						if(list.style!=""&&list.style!=undefined){
						var sty=JSON.parse(list.style);						
						context.save();
						context.beginPath();
						//context.clearRect(element.coordinate_x*scale, element.coordinate_y*scale, element.width*scale, element.height*scale);
						
						var TextObj=new Object;
						var fontsize=parseInt(Number(sty.size)*scale);
						if(fontsize<12){
							var newscale=12/fontsize;
							TextObj={
								x:parseInt(element.coordinate_x*scale*newscale),
								y:parseInt(element.coordinate_y*scale*newscale),
								width:parseInt(element.width*scale*newscale),
								height:parseInt(element.height*scale*newscale),
								family:'Microsoft YaHei',							
								size:12+"px",
								weight:'',
								color:sty.color.toString(),
								style:'',
								center:sty.align!=''?sty.align.toString():"center",
								lineHeight:parseInt((Number(sty.size)*Number(sty.line_space))*scale*newscale),
								text:list.content,
								transform:sty.direction*90,
								newscale:newscale,
								format:sty.format,//lym 2017 11.22 新增月历修改
								ischange:element.ischange
							}
							
						}else{
						 TextObj={
							x:parseInt(element.coordinate_x*scale),
							y:parseInt(element.coordinate_y*scale),
							width:parseInt(element.width*scale),
							height:parseInt(element.height*scale),
							family:'Microsoft YaHei',							
							size:parseInt(Number(sty.size)*scale)+"px",
							weight:'',
							color:sty.color.toString(),
							style:'',
							center:sty.align!=''?sty.align.toString():"center",
							lineHeight:parseInt((Number(sty.size)*Number(sty.line_space))*scale),
							text:list.content,
							transform:sty.direction*90,
							newscale:1,
							format:sty.format,//lym 2017 11.22 新增月历修改
							ischange:element.ischange
							}
						}
						
						if(TextObj.transform>0){
							if(TextObj.transform==90||TextObj.transform==270){
								var w=TextObj.height;
								var h=TextObj.width;
								TextObj.width=w;
								TextObj.height=h;
							}
						}
						//var reg=new RegExp("[↵  ↵]","g");
						//var reg=new RegExp(" ↵","g");
						//TextObj.text=TextObj.text.toString().replace(reg,'');
						//var re=new RegExp("↵","g");
						//TextObj.text=TextObj.text.toString().replace(re,'');
						//TextObj.text=TextObj.text.toString();
						
						drawlinetext(TextObj,function(result,strenter,data){
							if(TextObj.format==""){
								UtilProperty[w_id].worker[i].control_list[k].element_list[0].content=result.join(strenter);	
							}
							
							if(TextObj.transform>0){
								textRoate(data,TextObj.width,TextObj.height,context,TextObj.transform,element.coordinate_x*scale,element.coordinate_y*scale,TextObj.newscale);
							}else{
							//context.drawImage(data,element.coordinate_x*scale,element.coordinate_y*scale,element.width*scale/TextObj.newscale, element.height*scale/TextObj.newscale);
							context.drawImage(data,element.coordinate_x*scale,element.coordinate_y*scale,element.width*scale, element.height*scale);
							
							}
						
						context.rect(element.coordinate_x*scale, element.coordinate_y*scale, element.width*scale, element.height*scale);
						context.closePath();
						context.save();
						context.clip();	
						context.restore();	
							
						});
							
				}else{
					console.log(list.style==""?'list.style不能为空':'list.style 不能为 undefined')
				}
				}else{
					if(flag){
					var fontcss='bold  x-large Microsoft YaHei';
					//在canvas中增加提示 					
 					createReactTips(context,element,scale,'字',fontcss);
 					}
				}
			}else if(element.control_id==3){//lym 2017 11.17 新增月历修改
				if(element.element_list instanceof Array&&element.element_list.length>0){
					calendarTocanvas(context,element,element.element_list[0].date_list,scale)//日历单独生成新的canvas					
					
				}
			}else if(element.control_id==4){//lym 2017 12.11 新增一图一记修改
				if(element.element_list instanceof Array&&element.element_list.length>0){					
					weatherdata(context,w_id,element,scale)
				}
			}
			
			
	}
	
	
}


//元素旋转
function originalcanvasimgrote(context,_elementlist){			
		_elementlist.transform = _elementlist.transform % 360;
	
		var w = _elementlist.img.width;
		var h = _elementlist.img.height;
	
		var x = w / -2;
		var y = h / -2;
	
		if(_elementlist.transform % 180 != 0) {
			create_cvs.width = h;
			create_cvs.height = w;
			if(w > h) {
				//console.log(1);
				_elementlist.imgw = _elementlist.w;
				_elementlist.imgh = _elementlist.w / h * w;
			} else {
				//console.log(2);
				_elementlist.imgh = _elementlist.h;
				_elementlist.imgw = _elementlist.h / w * h;
			}
		} else {
			create_cvs.width = w;
			create_cvs.height = h;
			if(w > h) {
				//console.log(3);
				_elementlist.imgh = _elementlist.h;
				_elementlist.imgw = _elementlist.h / h * w;
			} else {
				//console.log(4);
				_elementlist.imgw = _elementlist.w;
				_elementlist.imgh = _elementlist.w / w * h;
			}
		}
	 	create_cvs_2d.clearRect(0, 0, create_cvs_2d.width, create_cvs_2d.height);
		create_cvs_2d.translate(create_cvs.width / 2, create_cvs.height / 2);
		create_cvs_2d.rotate(_elementlist.transform * Math.PI / 180);
		create_cvs_2d.drawImage(_elementlist.img, x, y);
	
		//防止旋转图片后，指定画图区留白（图片小于画图区）  
		if(_elementlist.imgw < _elementlist.w) {
			_elementlist.imgh = _elementlist.w / _elementlist.imgw * _elementlist.imgh;
			_elementlist.imgw = _elementlist.w;
		}
		if(_elementlist.imgh < _elementlist.h) {
			_elementlist.imgw = _elementlist.h / _elementlist.imgh * _elementlist.imgw;
			_elementlist.imgh = _elementlist.h;
		}
	
		/*_elementlist.imgx = _elementlist.x;
		_elementlist.imgy = _elementlist.y;*/
		drawImgByStatus(context,_elementlist)
		
	
		
	};	
	

//绘图
function drawImgByStatus(context,_elementlist){	
	if(_elementlist.image_id==6873){
		console.log(_elementlist.x, _elementlist.y, _elementlist.w, _elementlist.h)
		}
		context.beginPath();
		//防止黑色背景出现
		//context.clearRect(_elementlist.x, _elementlist.y, _elementlist.w, _elementlist.h);
		context.rect(_elementlist.x, _elementlist.y, _elementlist.w, _elementlist.h);
		context.save();
		context.clip();		    
		if(!_elementlist.imgw) {
			//设置画入画布内的图片宽、高（等比例铺满指定盒子，溢出被剪切掉）    
			if(_elementlist.imgoldw / _elementlist.imgoldh > _elementlist.w / _elementlist.h) {
				_elementlist.imgh = _elementlist.h;
				_elementlist.imgw = _elementlist.h / _elementlist.imgoldh * _elementlist.imgoldw;
			} else {
				_elementlist.imgw = _elementlist.w;
				_elementlist.imgh = _elementlist.w / _elementlist.imgoldw * _elementlist.imgoldh;
			}
		}
		if(_elementlist.transform!=0) {	
			context.drawImage(create_cvs, _elementlist.imgx, _elementlist.imgy, _elementlist.imgw, _elementlist.imgh);
		} else{			
			context.drawImage(_elementlist.img, _elementlist.imgx, _elementlist.imgy, _elementlist.imgw, _elementlist.imgh);
			
		}
		context.restore();
	};

//在canvas中增加提示 
function createReactTips(context,element,scale,text,fontcss){
	context.beginPath();
	//设置画入画布内的图片宽、高（等比例铺满指定盒子，溢出被剪切掉）    
	//context.clearRect(element.coordinate_x * scale, element.coordinate_y * scale, element.width * scale, element.height * scale);
	
	if(element.control_id==1){
		context.rect(element.coordinate_x * scale, element.coordinate_y * scale, element.width * scale, element.height * scale);
		context.closePath();
		context.fillStyle = '#c1c1c1';
		context.fill();
		var r = 0;
		if(element.width > element.height) {
			r = element.width * scale / 2 / 2
		} else {
			r = element.height * scale / 2 / 2
		}
		if(r > 20) {
			r = 20
		}
		context.beginPath();
		context.arc(element.coordinate_x * scale + element.width * scale / 2, element.coordinate_y * scale + element.height * scale / 2, r, 0, Math.PI * 2);
		context.strokeStyle = "#878787";
		context.lineWidth = 1;
		context.closePath();
		context.stroke();
		
		context.beginPath();
		context.fillStyle = '#878787';
		context.textAlign = "center";
		context.textBaseline = "middle";
		context.font = fontcss;
		context.fillText(text, element.coordinate_x * scale + element.width * scale / 2, element.coordinate_y * scale + element.height * scale / 2);
		context.closePath();
		context.save();
		context.clip();
		context.restore();
	}else if(element.control_id==2){
		context.beginPath();
		context.setLineDash([5, 9]);
		context.strokeStyle = "#6c6c6b";
		context.strokeRect(element.coordinate_x * scale, element.coordinate_y * scale, element.width * scale, element.height * scale);
		context.closePath();
		context.save();
		context.clip();
		context.restore();
	}
}


//得到点击的区域
function originalTouchEnd(event){
	event.preventDefault();
	try{
			wx.hideOptionMenu();//隐藏分享菜单		
		}catch(e){
			//TODO handle the exception
		}
	var canvas=document.getElementById('originalcanvas');
	var context=canvas.getContext('2d');
	var _touch=null;
	if(event.changedTouches){
	 _touch=event.changedTouches[0];
	}else{
		_touch={clientX:event.clientX,clientY:event.clientY};
	}
		
	var x=_touch.clientX-canvas.getBoundingClientRect().left;
	var y=_touch.clientY-canvas.getBoundingClientRect().top;
	var pageno=$("#originalcanvas").attr("page");
	var tempitem=UtilProperty[work_id].worker[pageno];	
	var canvasData=tempitem.control_list;
	var w=boxwindthheight.width;
	var h=boxwindthheight.height;
		var o_scale;
		
		if(w>h){			
			 o_scale=canvas.height/Pixel_Ratio/tempitem.height;
		}else{			
			 o_scale=canvas.width/Pixel_Ratio/tempitem.width;
		}
	var currentid="";//当前被点击中的控件id;	
	var currentType="";//当前被点击中的控件类型;	
	ctxmsg={};
	ctxtext={};
	ctxdate={};
	for(var i=0; i<canvasData.length;i++){	
		var item=canvasData[i];		
		/*var dx=(item.coordinate_x+item.width)*o_scale-x;
		var dy=(item.coordinate_y+item.height)*o_scale-y;*/
		var dx=(item.coordinate_x+item.width)*o_scale;
		var dy=(item.coordinate_y+item.height)*o_scale;
		var x1=item.coordinate_x*o_scale;
		var y1=item.coordinate_y*o_scale;		
		
		var scale;
			
			if((item.width/item.height)>(w/h)){			
				 scale=w/item.width;
			}else{
				scale=h/item.height;
			}
			if(item.control_id==1&&(item.element_list instanceof Array&&item.element_list.length>0)){
				var list=item.element_list[0];
				var pic=UtilProperty[work_id].imageObj[list.image_url];
				ctxmsg[item.id] = {
						imgurl:list.image_url,
						img:pic,
						image_id:list.image_id,
						x: 0,
						y: 0,
						w: item.width*scale,
						h: item.height*scale,
						imgx: list.cut_x*scale,
						imgy: list.cut_y*scale,
						imgw: 0,
						imgh: 0,
						imgoldx: 0,
						imgoldy: 0,
						imgoldw: pic.width,
						imgoldh: pic.height,
						imgwid:list.width,
						imghei:list.height,
						transform:item.element_list[0].transform,						
						scale:scale,						
						ischange:item.ischange,
						disabled:item.disabled
					};
			}else{
				ctxmsg[item.id]=new Object;
				ctxmsg[item.id].disabled=item.disabled;
			}
			if(item.control_id==2&&(item.element_list instanceof Array&&item.element_list.length>0)){
				var list=item.element_list[0];	
				if(list.style!=""&&list.style!=undefined){
				var sty=JSON.parse(list.style);
				ctxtext[item.id] = {
						content:list.content,
						id:list.id,	
						count:Number(sty.max),
						family:'Microsoft YaHei',							
						size:Number(sty.size),
						weight:'',
						color:sty.color.toString(),
						style:'',
						align:sty.align!=''?sty.align.toString():"center",						
						transform:sty.direction*90,	
						lineHeight:sty.line_space,
						scale:scale,
						ischange:item.ischange,
						disabled:item.disabled
					};
				}else{
					ctxtext[item.id]=new Object;
					ctxtext[item.id].disabled=item.disabled;
					ctxtext[item.id].count=10;
				}
			}else{
				ctxtext[item.id]=new Object;
				ctxtext[item.id].disabled=item.disabled;
				ctxtext[item.id].count=10;
			}
			//lym 2017 11.20 新增月历修改
			if(item.control_id==3&&(item.element_list instanceof Array&&item.element_list.length>0)){
				var list=item.element_list[0];	
				if(list.style!=""&&list.style!=undefined){
				ctxdate[item.id]=new Object;
				ctxdate[item.id].disabled=item.disabled;
				
				}
			}
			
			//lym 2017 12.15     新增一图一记修改
			if(item.control_id==4&&(item.element_list instanceof Array&&item.element_list.length>0)){
				var list=item.element_list[0];	
				if(list.style!=""&&list.style!=undefined){
				ctxweather[item.id]=new Object;
				ctxweather[item.id].disabled=item.disabled;
				var list=item.element_list[0];
				ctxweather[item.id].date=list.content.date;				
				ctxweather[item.id].icon=list.content.icon;				
				ctxweather[item.id].location=list.content.location;
				ctxweather[item.id].weaid=list.content.weaid;
				ctxweather[item.id].wea=list.content.wea;
				}
			}
			
			//if(dx>=0&&dy>=0){
			if((x1-x<=0&&dx-x>=0)&&(y1-y<=0&&dy-y>=0)){	
				if(item.control_id==1){					
					
					if(ctxmsg.hasOwnProperty(item.id)){
						currentid=item.id;
						currentType=item.control_id;
						//break;
					}
				}else if(item.control_id==2){
					
					
					if(ctxtext.hasOwnProperty(item.id)){
						currentid=item.id;
						currentType=item.control_id;
						//break;
					}
				}else if(item.control_id==3){//lym 2017 11.20 新增月历修改	
					
					if(ctxdate.hasOwnProperty(item.id)){
						currentid=item.id;
						currentType=item.control_id;
						//break;
					}
				}else if(item.control_id==4){//lym 2017 12.15   新增一图一记修改
					
					if(ctxweather.hasOwnProperty(item.id)){
						currentid=item.id;
						currentType=item.control_id;
						//break;
					}
				}
			}
		}
	if(currentType==1){
		if(ctxmsg[currentid].disabled==0&&ctxmsg[currentid].imgurl){
			if(currentid!=""){
				enterDrawbefore(currentid);
			}else{
				$(".dialog_tips").show().find(".dialog_txt").text("请填充完整再编辑");	
				setTimeout(function(){$(".dialog_tips").hide()},1000)
			}
			
		}else if(ctxmsg[currentid].disabled==0){		
			$('#js-file').attr("onfunction","0");
			$("#originalcanvas").attr("currentid",currentid);			
			//selectFileToUpload();
            $("#js-file").trigger("click");     
			
		}
	}else if(currentType==2&&ctxtext[currentid].disabled==0){				
		Drawtextopen(currentid,scale);//打开文字编辑区域进行编辑
		
	}else if(currentType==3&&ctxdate[currentid].disabled==0){//lym 2017 11.20 新增月历修改				
		//更换月历控件布局
		$(".comfirm_date").attr("currentid",currentid);
		var curentdate=new Date; 		
		$("#layoutdate").picker("setValue", [curentdate.getFullYear()+"年", (curentdate.getMonth()+1)+"月"]);
		$(".selectdate").show();
	}else if(currentType==4&&ctxweather[currentid].disabled==0){//lym 2017 12.12 新增一图一记修改			
		//更换一图一记控件布局
		$(".save_weather").attr("currentid",currentid);		
		weatherpopup(ctxweather[currentid].date,ctxweather[currentid].location,ctxweather[currentid].weaid);
	}
}

//进入编辑图片页面前执行的操作
function enterDrawbefore(currentid){
	
	$("#editorpage").empty();
	for(var i in ctxmsg){
	if(ctxmsg[i].imgurl&&ctxmsg[i].disabled==0){		
		var list=ctxmsg[i]		
			var scale=$("#editorpage").height()/list.h;
			var styletxt='width: '+list.h*scale+'px; height: '+list.h*scale+'px; background-image:url('+list.img.src+')';
			var strw=list.img.width>list.img.height?'style="height:100%;"':'style="width:100%;"';
			var html='<div class="editoritem itemflex  '+(i==currentid?"editoring":"")+'" currentid="'+i+'"   style="'+styletxt+'"></div>';
			$("#editorpage").append(html);
		
		
		}
	}
	
	canvas_img.width=ctxmsg[currentid].w;
	canvas_img.height=ctxmsg[currentid].h;	
	canvas_img.setAttribute("currentid",currentid);
	canvasdrag();
	canvas_img.setAttribute('style','width:'+canvas_img.width/Pixel_Ratio+'px; height:'+canvas_img.height/Pixel_Ratio+'px;')
	$(".originalwork").hide();
	$(".editor_canvas").show();
	
}


//改变大的canvas 的布局及图片
function changeoriginalcanvas(index){
		$("#originaleditor").empty();
		//var canvas= document.getElementById('originalcanvas');
		var canvas= document.createElement("canvas");
			canvas.id="originalcanvas";
			canvas.setAttribute("page",index);
			$("#originaleditor").append(canvas);
		var item=UtilProperty[work_id].worker[index];
		var context=canvas.getContext("2d");			
		var w=boxwindthheight.width;
		var h=boxwindthheight.height;		
		var scale;		
		if(item.width / item.height > w / h) {
			scale=w/item.width;
		} else {
			scale=h/item.height;
		}		
		canvas.height=item.height*scale;
		canvas.width=item.width*scale;		
		$("#originaleditor").append(canvas);
		canvas.setAttribute('style','width:'+canvas.width/Pixel_Ratio+'px; height:'+canvas.height/Pixel_Ratio+'px;');
		//$('#originaleditor').css({"width":item.width*scale,"height":item.height*scale});	
		
		//给相应的作品添加背景图片
		context.rect(0, 0, item.width, item.height);
		context.fillStyle = 'rgba(250,250,250,1)';
		context.fill();	
		context.save();	
		context.closePath();		
		//给相应的作品添加背景图片
		layoutbackground(item,context,canvas.width,canvas.height,scale);	
		
		drawToCanvas(work_id,canvas,index,item,context,scale,true,false);
		
		//document.getElementById('originalcanvas').addEventListener("touchend", originalTouchEnd, false);
	 	document.getElementById('originalcanvas').addEventListener("click", originalTouchEnd, false);
	
	
}
//改变小的canvas布局 
function changepagemainbox(index){	
	var item = UtilProperty[work_id].worker[index];	
	UtilProperty[work_id].base64image[index].imgurl=document.getElementById('originalcanvas').toDataURL("image/jpeg");
	$("#pagemain .page_item").eq(index).find('img').attr('src',UtilProperty[work_id].base64image[index].imgurl);
}


function Drawtextopen(currentid,scale){	
	
	if(ctxtext[currentid].hasOwnProperty('size')){
		var item=ctxtext[currentid];
		$(".area_input").attr({"currentid":currentid,"scale":scale,"maxlength":item.count});
		var reg=new RegExp(" ↵","g");
		item.content=item.content.replace(reg,'');
		var re=new RegExp("↵","g");
		item.content=item.content.replace(re,'');		
		if(item.content.length>item.count){
			item.content=item.content.substring(0,item.count);
		}
		$(".area_input").val(item.content);
		
	}else{		
		
		$(".area_input").attr({"currentid":currentid,"scale":scale,"maxlength":ctxtext[currentid].count});
		$(".area_input").val('');
	}
	$('.text_area_num').html('(限制'+ctxtext[currentid].count+'字以内)');
	$("#editortextarea").popup();
	$("#originalcanvas,.save_text").attr("currentid",currentid);
	
	
}
//把编辑过的文字回填
 function comfrimTextToCanvas(){
 		var k=$(".area_input").attr("currentid");
 		var scale=$(".area_input").attr("scale");
		var index= $("#originalcanvas").attr("page");
		var canvasData=UtilProperty[work_id].worker[index].control_list;		
		for(var i=0; i<canvasData.length;i++){			
				if(k==canvasData[i].id){
					if(!(JSON.stringify(ctxtext[k]) == "{}")){	
						var element=UtilProperty[work_id].worker[index].control_list[i];
					if(element.element_list instanceof Array&&element.element_list.length==0){
						UtilProperty[work_id].worker[index].control_list[i].element_list[0]=new Object;
					}
					UtilProperty[work_id].worker[index].control_list[i].ischange=true;
					UtilProperty[work_id].worker[index].control_list[i].element_list[0].content=$(".area_input").val();					
													
					}
				}
		
		}
		ctxtext ={};	
		changeoriginalcanvas(index);		
		changepagemainbox(index);
 }


//把canvas保存图片并预览

function saveCanvasOper(){//layout_id 为0 时不更改布局， 否则更改了布局
	UtilProperty[work_id].update_work_page=new Array;	
	var worker=UtilProperty[work_id].worker;
	for(i=0; i<worker.length;i++){		
		var item=worker[i];	
		var updatelist=new Array;
		var layout_id;		
		for(k=0; k<item.control_list.length;k++){
			var element=item.control_list[k];
			layout_id=element.layout_id;
			if(element.control_id==1){				
				if(element.ischange||layout_id!=undefined){//lym 2017 11.24 修改内容被清空
					var up_obj=new Object;
					if(element.element_list instanceof Array&&element.element_list.length>0){									
						var list=element.element_list[0];
						up_obj={image_id:list.image_id,width:list.width,height:list.height,cut_x:list.cut_x,cut_y:list.cut_y,transform:list.transform,style:list.style};
					}	
					var updata_obj={source_control_id:element.id,control_id:element.control_id,content:up_obj}							
					updatelist.push(updata_obj);
				}


				
			}else if(element.control_id==2){	
				if(element.ischange||layout_id!=undefined){//lym 2017 11.24 修改内容被清空
					var updata_obj={source_control_id:element.id,control_id:element.control_id}
					if(element.element_list instanceof Array&&element.element_list.length>0){	
						updata_obj.content={content:element.element_list[0].content,style:element.element_list[0].style};
						
					}
												
					updatelist.push(updata_obj);
				}
			}else if(element.control_id==3){//lym 2017 11.17 新增月历修改	
				if(element.ischange||layout_id!=undefined){//lym 2017 11.24 修改内容被清空
					var updata_obj={source_control_id:element.id,control_id:element.control_id}
					if(element.element_list instanceof Array&&element.element_list.length>0){	
						updata_obj.content={content:element.element_list[0].content,style:element.element_list[0].style};
						
					}
												
					updatelist.push(updata_obj);
				}
			}else if(element.control_id==4){//lym 2017 12.15   新增一图一记修改
				if(element.ischange||layout_id!=undefined){//lym 2017 11.24 修改内容被清空
					var updata_obj={source_control_id:element.id,control_id:element.control_id}
					if(element.element_list instanceof Array&&element.element_list.length>0){	
						updata_obj.content={content:JSON.stringify(element.element_list[0].content),style:element.element_list[0].style};
						
					}
												
					updatelist.push(updata_obj);
				}
			}
		
			
		}
		if(updatelist.length>0||item.background.ischange==true){
			var un_obj=new Object;
			un_obj.page_id=item.id;
			un_obj.background_id=item.background_id;
			if(layout_id&&updatelist.length>0){
				un_obj.layout_id=layout_id;			
			}else{
				un_obj.layout_id=0;	
			}
			un_obj.control_list=updatelist;
			
			UtilProperty[work_id].update_work_page.push(un_obj);
		}
	
	
	}
	/*for(var i=0; i<UtilProperty[work_id].base64image.length;i++){
		var item=UtilProperty[work_id].base64image[i];
		$('.onfoucs .placeholder[title="'+item.title+'"]').html('<img src="'+item.imgurl+'" alt="">');
	}*/
	if(UtilProperty[work_id].update_work_page.length>0&&tempmodel!=-1){
		if(ismodel!=0){//是否选择了不再提醒
			/*if(window.localStorage){
				ismodel=Number(tempmodel);
				localStorage["ismodel"] = JSON.stringify(ismodel);
			}*/
			ismodel=Number(tempmodel);
			$.cookie('ismodel', ismodel, { expires:3, path: '/' }); 
		}
	}
	$(".onfoucs .work_input").prop("checked",true);
	console.log("合并更新的数据")
	console.log(UtilProperty[work_id].update_work_page);

	
}



//canvas保存图片成功后修改ischange的值
function changeischange(){
	UtilProperty[work_id].update_work_page=new Array;	
	var worker=UtilProperty[work_id].worker;
	
	for(i=0; i<worker.length;i++){		
		var item=worker[i];		
		UtilProperty[work_id].worker[i].background.ischange=false;
		for(k=0; k<item.control_list.length;k++){			
			UtilProperty[work_id].worker[i].control_list[k].ischange=false;
		}
		
	
	}
		
}


//添加图片
 function addimgaetocanvas(imgurl,image_id,width,height){
	
	var srcurl = imgurl+imgsize_str;
	if(UtilProperty[work_id].imageObj[srcurl]==undefined){	// lym 2018.01.02 修改 图片加载 原先的的id标志转为url标志	
		var mulitImg=new Array;		
		mulitImg.push(srcurl);				
		imagelistToObject(mulitImg,work_id,function(){
			//打开编辑作品页面并且合成相应的页面
			getnewimage(image_id,srcurl,width,height);

		})
		
	}else{
		//已经存在的照片 直接 修改 UtilProperty和当前的 相册页
		getnewimage(image_id,imgurl,width,height);
	}
}
//添加图片时修改 UtilProperty和当前的 相册页
function getnewimage(image_id,imgurl,width,height){
		var canvas= document.getElementById('originalcanvas');	
		var pageno=canvas.getAttribute("page");		
		var canvasData=UtilProperty[work_id].worker[pageno].control_list;
		var currentid=canvas.getAttribute("currentid");
		var work_page_id=UtilProperty[work_id].worker[pageno].id;
		for(var i=0; i<canvasData.length;i++){
			var item = canvasData[i];
			if(item.id==currentid){	
				UtilProperty[work_id].worker[pageno].control_list[i].ischange=true;
				UtilProperty[work_id].worker[pageno].control_list[i].element_list=new Array;
				var obj={
						id: '',
						work_page_id: work_page_id,
						image_id: image_id,						
						image_url:imgurl,
						width: width,
						height: height,
						cut_x: 0,
						cut_y: 0,
						transform: 0.00,
						style: ""
					}
				if(!item.layout_id){
					obj.work_control_id=item.id;
				}
				UtilProperty[work_id].worker[pageno].control_list[i].element_list.push(obj);
			break;
			}
		}
		changeoriginalcanvas(pageno);
		changepagemainbox(pageno);
		$("#loadimground").hide();
		$("#load p").html('loading');
		$(".up_page").hide();
		
	}

//保存图片编辑
function saveimageeditor(){	
		var scale=ctxmsg.scale;		
		var index= $("#originalcanvas").attr("page");		
		var canvasData=UtilProperty[work_id].worker[index].control_list;
		var currentid=canvas_img.getAttribute("currentid");
		for(var i=0; i<canvasData.length;i++){
			for(var k in ctxmsg){
				if(k==canvasData[i].id){	
					if(ctxmsg[k].scale){
					var scale=ctxmsg[k].scale;	
					UtilProperty[work_id].worker[index].control_list[i].ischange=ctxmsg[k].ischange;
					UtilProperty[work_id].worker[index].control_list[i].element_list[0].cut_x=ctxmsg[k].imgx/scale;
					UtilProperty[work_id].worker[index].control_list[i].element_list[0].cut_y=ctxmsg[k].imgy/scale;
					UtilProperty[work_id].worker[index].control_list[i].element_list[0].height=ctxmsg[k].imghei;
					UtilProperty[work_id].worker[index].control_list[i].element_list[0].image_id=ctxmsg[k].image_id;					
					UtilProperty[work_id].worker[index].control_list[i].element_list[0].image_url=ctxmsg[k].imgurl;
					UtilProperty[work_id].worker[index].control_list[i].element_list[0].transform=ctxmsg[k].transform;
					UtilProperty[work_id].worker[index].control_list[i].element_list[0].width=ctxmsg[k].imgwid;	
					UtilProperty[work_id].imageObj[ctxmsg[k].imgurl]=ctxmsg[k].img;
					}
				}
			}
		}
		ctxmsg ={};
		changeoriginalcanvas(index);
		$(".editor_canvas").hide();
		$(".originalwork").show();
		changepagemainbox(index);
}



